using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

public class HomeLevel : MonoBehaviour
{
	[Header("Home")]
	public RectTransform barTransfrom;

	[Header("Bar")]
	public Image rankImage;

	public Image xpBarImage;

	public Text xpText;

	public Text additionalPointsText;

	[Header("Popup")]
	public GameObject rankUpObject;

	public Transform rankUpPopup;

	public Image fadeImage;

	public Image rankUpMedal;

	public Image rankUpNextMedal;

	public Button rankUpButton;

	private Tween _displayTween;

	public void DisplayWithAnimation(bool state)
	{
		if (_displayTween != null)
		{
			_displayTween.Kill();
		}
		if (state)
		{
			_displayTween = barTransfrom.DOAnchorPosX(10f, 0.2f).SetEase(Ease.OutBack);
		}
		else
		{
			_displayTween = barTransfrom.DOAnchorPosX(-200f, 0.2f);
		}
	}

	public void SetReward(int xp)
	{
		additionalPointsText.text = $"+{xp}";
	}

	public void SetRank(int rank, int xp, RankManager.RankParameters parameters, RankManager.RankParameters nextParameters)
	{
		xpBarImage.color = parameters.mainColor;
		rankImage.sprite = parameters.icon;
		float fillAmount = (float)xp / (float)nextParameters.xpRequired;
		xpBarImage.fillAmount = fillAmount;
		xpText.text = $"{xp}/{nextParameters.xpRequired}";
	}

	public void UpdateRank(int xp, RankManager.RankParameters parameters)
	{
		additionalPointsText.gameObject.SetActive(value: true);
		additionalPointsText.color = new Color(1f, 1f, 1f, 0f);
		additionalPointsText.DOFade(1f, 0.2f);
		float endValue = (float)xp / (float)parameters.xpRequired;
		xpBarImage.DOFillAmount(endValue, 1f).OnUpdate(delegate
		{
			xpText.text = $"{Mathf.RoundToInt((float)parameters.xpRequired * xpBarImage.fillAmount)}/{parameters.xpRequired}";
		}).OnComplete(delegate
		{
			xpText.text = $"{xp}/{parameters.xpRequired}";
			additionalPointsText.DOKill();
			additionalPointsText.DOFade(0f, 0.2f).OnComplete(delegate
			{
				if ((bool)additionalPointsText)
				{
					additionalPointsText.gameObject.SetActive(value: false);
				}
			});
		});
	}

	public void LevelUpRank(int rank, int xp, RankManager.RankParameters parameters, RankManager.RankParameters nextParameters)
	{
		additionalPointsText.gameObject.SetActive(value: true);
		additionalPointsText.color = new Color(1f, 1f, 1f, 0f);
		additionalPointsText.DOFade(1f, 0.2f);
		xpBarImage.DOFillAmount(1f, 0.5f).OnUpdate(delegate
		{
			xpText.text = $"{Mathf.RoundToInt((float)parameters.xpRequired * xpBarImage.fillAmount)}/{parameters.xpRequired}";
		}).OnComplete(delegate
		{
			xpText.text = $"{parameters.xpRequired}/{parameters.xpRequired}";
			ShowRankUpPopup(parameters, nextParameters);
			xpBarImage.fillAmount = 0f;
			xpBarImage.color = nextParameters.mainColor;
			rankImage.sprite = nextParameters.icon;
			UpdateRank(RankManager.Instance.CachedXp, nextParameters);
			additionalPointsText.DOKill();
			additionalPointsText.DOFade(0f, 0.2f).OnComplete(delegate
			{
				if ((bool)additionalPointsText)
				{
					additionalPointsText.gameObject.SetActive(value: false);
				}
			});
		});
	}

	public void ShowRankUpPopup(RankManager.RankParameters rankParameters, RankManager.RankParameters nextParameters)
	{
		rankUpMedal.sprite = rankParameters.icon;
		rankUpNextMedal.sprite = nextParameters.icon;
		rankUpObject.SetActive(value: true);
		rankUpPopup.localScale = Vector3.zero;
		rankUpMedal.color = Color.white;
		rankUpNextMedal.color = new Color(1f, 1f, 1f, 0f);
		rankUpMedal.transform.localScale = Vector3.one;
		rankUpButton.interactable = false;
		fadeImage.color = new Color(0f, 0f, 0f, 0f);
		fadeImage.DOFade(20f / 51f, 0.3f);
		rankUpPopup.DOScale(Vector3.one, 0.3f).SetEase(Ease.OutBack).OnComplete(delegate
		{
			rankUpMedal.DOFade(0f, 0.5f).SetDelay(0.3f);
			rankUpMedal.transform.DOScale(2f, 0.5f).SetDelay(0.3f);
			rankUpNextMedal.DOFade(1f, 0.5f).SetDelay(0.3f).OnComplete(delegate
			{
				rankUpButton.interactable = true;
			});
		});
	}

	public void CALLBACK_BUTTON_CloseRankUpPopup()
	{
		rankUpButton.interactable = false;
		fadeImage.DOFade(0f, 0.3f);
		rankUpPopup.DOScale(Vector3.zero, 0.3f).SetEase(Ease.InBack).OnComplete(delegate
		{
			rankUpObject.SetActive(value: false);
		});
	}
}
